package com.genreate.util.pay.ali;

import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayTradeApi;
import com.alipay.v3.model.*;
import com.alipay.v3.util.model.CustomizedParams;
import com.genreate.util.pay.ali.AliPayConfigPo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统一收单交易查询
 *
 * @author LYS
 * @date 2025-07-08
 */
public class AlipayTradeApiQuery {

    public static void main(String[] args) throws ApiException {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        // 初始化alipay参数（全局设置一次）
        defaultClient.setAlipayConfig(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeApi api = new AlipayTradeApi();
        AlipayTradeQueryModel data = new AlipayTradeQueryModel();
        
        // 设置订单支付时传入的商户订单号
        data.setOutTradeNo("20150320010101001");
        
        // 设置查询选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("trade_settle_info");
        data.setQueryOptions(queryOptions);

        try {
            AlipayTradeQueryResponseModel response = api.query(data);
            System.out.println(response);
        } catch (ApiException e) {
            AlipayTradeQueryDefaultResponse errorObject = (AlipayTradeQueryDefaultResponse) e.getErrorObject();
            System.out.println("调用失败:" + errorObject);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        return AliPayConfigPo.getAppAlipayConfig();
    }
}
